﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsModel
    {
        private readonly KeRui.DAL.CmsModel dal = new KeRui.DAL.CmsModel();

        public int Add(KeRui.Model.CmsModel model)
        {
            return this.dal.Add(model);
        }

        public void ChageOrderID(int flag, int NowID)
        {
            new KeRui.DAL.Common().ChageOrderID(flag, NowID, "ModelID", "CmsModel ");
        }

        public List<KeRui.Model.CmsModel> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsModel> list = new List<KeRui.Model.CmsModel>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsModel item = new KeRui.Model.CmsModel();
                    if (dt.Rows[i]["ModelID"].ToString() != "")
                    {
                        item.ModelID = int.Parse(dt.Rows[i]["ModelID"].ToString());
                    }
                    item.ModelName = dt.Rows[i]["ModelName"].ToString();
                    if (dt.Rows[i]["ModelType"].ToString() != "")
                    {
                        item.ModelType = int.Parse(dt.Rows[i]["ModelType"].ToString());
                    }
                    item.Description = dt.Rows[i]["Description"].ToString();
                    item.ItemName = dt.Rows[i]["ItemName"].ToString();
                    item.ItemUnit = dt.Rows[i]["ItemUnit"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsUsing"].ToString() == "1") || (dt.Rows[i]["IsUsing"].ToString().ToLower() == "true"))
                        {
                            item.IsUsing = true;
                        }
                        else
                        {
                            item.IsUsing = false;
                        }
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int ModelID)
        {
            return this.dal.Delete(ModelID);
        }

        public List<KeRui.Model.CmsModel> GetList()
        {
            DataSet set = new KeRui.DAL.Common().GetList("CmsModel", -1, -1, string.Empty, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public int GetMaxOrderID()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetMaxOrderID("CmsModel ", "OrderID");
        }

        public KeRui.Model.CmsModel GetModel(int ModelID)
        {
            return this.dal.GetModel(ModelID);
        }

        public KeRui.Model.CmsModel GetModelByCache(int ModelID)
        {
            string cacheKey = "CmsModelModel-" + ModelID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ModelID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsModel) cache;
        }

        public List<KeRui.Model.CmsModel> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public DataSet GetSelect()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsModel", "", " ModelType desc", " ModelID,ModelName");
        }

        public int Update(KeRui.Model.CmsModel model)
        {
            return this.dal.Update(model);
        }
    }
}

